home *** CD-ROM | disk | FTP | other *** search
Wrap
Java Source | 2001-06-23 | 6.0 KB | 195 lines
/* This file and its intellectual contents are considered property of the creator and are to be treated as such with respect to use in other applications. Any use of this software for any purpose whatsoever must have explicit permission from the author of the file. This code is part of an ongoing development process for future possible products, and so is considered private property. Do not use without permission. Author: Scott C. Ziegler <Aslan@Narnia.net> */ package Arcana; import java.awt.*; import java.io.*; import java.util.*; import java.awt.event.*; import java.awt.Color; // SpellRegisterVector // // Primary data-structure for the storage and manipulation of // SpellRegisterRecord entries public class SpellRegisterVector implements Serializable { static final int BY_SCHOOL = 0; static final int BY_LEVEL = 1; static final int BY_COMPONENTS = 2; Vector SpellRegisterVec; // Constructors public SpellRegisterVector() { SpellRegisterVec = new Vector(50); } public SpellRegisterVector(SpellRecord[] spellArray) { SpellRegisterVec = new Vector(spellArray.length); for (int i=0; i < spellArray.length; i++) { SpellRegisterVec.addElement(spellArray[i].getSpellRegister()); } } // the Vector parameter MUST BE of SpellRegisters or the program will malfunction public SpellRegisterVector(Vector initSpellVector) { SpellRegisterVec = new Vector(initSpellVector.size()); setSpellRegisterVector(initSpellVector); } // Selectors public Vector getSpellRegisterVector() { return SpellRegisterVec; } public SpellRegister getSpellRegister(int index) { return (SpellRegister)SpellRegisterVec.elementAt(index); } public Vector getSpellNamesVector() { Vector vec = new Vector(SpellRegisterVec.size()); Enumeration e = SpellRegisterVec.elements(); SpellRegister sentinel; // holds SpellRegister to while (e.hasMoreElements()) { sentinel = (SpellRegister)e.nextElement(); vec.addElement(sentinel.getName()); } return vec; } // Mutators // function - take spellvector as an argument and reassign the SpellRegister with it public void setSpellRegisterVector(Vector spellvector) { java.util.Enumeration e = spellvector.elements(); if (SpellRegisterVec.size() > 0) { SpellRegisterVec.removeAllElements(); } SpellRegister Sentinel; while (e.hasMoreElements()) { // this line makes sure that non-SpellRegisters don't get through Sentinel = (SpellRegister)e.nextElement(); // as Sentinel can only be of type SpellRegister. SpellRegisterVec.addElement(Sentinel); } } // function - take spellvector as an argument and append it to the SpellRegister public void appendSpellVector(Vector spellvector) { java.util.Enumeration e = spellvector.elements(); SpellRegister Sentinel; while (e.hasMoreElements()) { Sentinel = (SpellRegister)e.nextElement(); SpellRegisterVec.addElement(Sentinel); } } // functions - adds spell(s) to vector in alphabetical order (by reordering Vector) public void addSpell(SpellRecord spell) { SpellRegisterVec.addElement(spell.getSpellRegister()); SpellRegister[] sortingArray = new SpellRegister[SpellRegisterVec.size()]; SpellRegisterVec.copyInto(sortingArray); Sorter.sort(sortingArray); // SpellRegisterVec = new Vector(sortingArray); this.setSpellRegisterVector(this.arrayToVector(sortingArray)); } public void addSpells(SpellRecord[] spell) { SpellRegister[] sortingArray = new SpellRegister[SpellRegisterVec.size() + spell.length]; // this loop takes the news spells and inserts them into the new // spaces on the end of the sortingArray. for (int i = SpellRegisterVec.size(), j = 0; i < (spell.length + SpellRegisterVec.size()); i++, j++) { sortingArray[i] = spell[j].getSpellRegister(); } Sorter.sort(sortingArray); this.setSpellRegisterVector(this.arrayToVector(sortingArray)); } public void removeSpell(SpellRecord spell) { if (!SpellRegisterVec.removeElement(spell.getSpellRegister())) { // JOptionPane ... ERROR! } this.sortSpells(); } // Methods public Vector arrayToVector(Object[] objArray) { Vector newVec = new Vector(objArray.length); for (int i = 0; i < objArray.length; i++) { newVec.addElement(objArray[i]); } return newVec; } // function - reorders the vector in alphabetical order according to name public void sortSpells() { SpellRegister[] sortingArray = new SpellRegister[SpellRegisterVec.size()]; SpellRegisterVec.copyInto(sortingArray); Sorter.sort(sortingArray); this.setSpellRegisterVector(this.arrayToVector(sortingArray)); } // function - reorders the vector by criteria (school/level/Component) public void sortByField(int field) { SpellRegister[] sortingArray = new SpellRegister[SpellRegisterVec.size()]; SpellRegisterVec.copyInto(sortingArray); switch (field) { case BY_SCHOOL : Sorter.sort(sortingArray, new Sorter.Comparer() { public int compare(Object a, Object b) { SpellRegister adin = (SpellRegister)a, dva = (SpellRegister)b; if ((adin.getSchool()).compareTo(dva.getSchool()) == 0) return (adin.getName()).compareTo(dva.getName()); else return (adin.getSchool()).compareTo(dva.getSchool()); } }); break; case BY_LEVEL : Sorter.sort(sortingArray, new Sorter.Comparer() { public int compare(Object a, Object b) { SpellRegister adin = (SpellRegister)a, dva = (SpellRegister)b; if (adin.getLevel() == dva.getLevel()) return (adin.getName()).compareTo(dva.getName()); else return (adin.getLevel() - dva.getLevel()); } }); break; case BY_COMPONENTS : Sorter.sort(sortingArray, new Sorter.Comparer() { public int compare(Object a, Object b) { SpellRegister adin = (SpellRegister)a, dva = (SpellRegister)b; if (adin.getComponents() == dva.getComponents()) return (adin.getName()).compareTo(dva.getName()); else return (adin.getComponents() - dva.getComponents()); } }); break; } } }